﻿using Blog.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Blog.Model;
using System.Data.SqlClient;
namespace Blog.DAL
{
  public  class Article_DAL:IArticle
    {
        public static List<Article> totalArticle; 
        public static List<Article> Select() { 
        //实现查找方法
            string sql = "select * from T_Article";
            totalArticle=new List<Article>();
            DataTable dt = SqlHelper.GetTable(sql, CommandType.Text, null);
            foreach (DataRow item in dt.Rows)
            {
                Article article = new Article();
                LoadEntity(item, article);
                totalArticle.Add(article);
            }
            return totalArticle;

        }
        public static void LoadEntity(DataRow row,Article info)
        {
            info.ID = Convert.ToInt32(row["ID"]);
            info.Title = row["Title"] != DBNull.Value ? row["Title"] .ToString(): "没有";
            info.Content = row["Content"]!=DBNull.Value? row["Content"].ToString():"没有";
            info.Like = row["Like"] != DBNull.Value ? Convert.ToInt32(row["Like"]) :0;
            info.Views =row["Views"] != DBNull.Value ?Convert.ToInt32(row["Views"]):0;
            info.Time =row["Time"] != DBNull.Value ? Convert.ToDateTime(row["Time"]):new DateTime();
        }


        public void Add(Article article)
        {
            SqlParameter[] param ={
                new SqlParameter("@Content",SqlDbType.NVarChar),
                              };
            param[0].Value = article.Content;
            string re = "insert into T_Article (Content) values(@Content)";
            SqlHelper.ExecuteNonquery(re,CommandType.Text,param);
        }

        public void Delete()
        {
            throw new NotImplementedException();
        }

        public void Change()
        {
            throw new NotImplementedException();
        }

        void IArticle.Select()
        {
            throw new NotImplementedException();
        }
    }
}
